<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace form;
/**
 * 时间轴
 */
class TimeLine extends Html
{


    /**
     * 尺寸
     */
    const SIZE_SM     = 0;
    const SIZE_NORMAL = 1;
    const SIZE_LG     = 2;
    const SIZE_XL     = 3;
    /**
     * 颜色
     */
    const COLOR_PRIMARY   = 0;
    const COLOR_SUCCESS   = 1;
    const COLOR_INFO      = 2;
    const COLOR_WARNING   = 3;
    const COLOR_DANGER    = 4;
    const COLOR_DARK      = 5;
    const COLOR_SECONDARY = 6;
    const COLOR_PURPLE    = 7;
    const COLOR_PINK      = 8;
    const COLOR_CYAN      = 9;
    const COLOR_YELLOW    = 10;
    const COLOR_BROWN     = 11;
    const COLOR_LIGHT     = 12;
    const COLOR_MUTED     = 13;
    protected $data = [];

    protected $dotColorMap = [
        'badge-primary',
        'badge-success',
        'badge-info',
        'badge-warning',
        'badge-danger',
        'badge-dark',
        'badge-secondary',
        'badge-purple',
        'badge-pink',
        'badge-cyan',
        'badge-yellow',
        'badge-brown',
        'badge-light',
        'badge-muted',
    ];

    protected $dotSize = 'badge-dot';

    protected $dotColor = 'badge-primary';

    private $dotSizeMap = [
        'badge-dot-sm', 'badge-dot', 'badge-dot-lg', 'badge-dot-xl'
    ];

    public function size(int $size): self
    {
        $this->dotSize = $this->dotSizeMap[$size];
        return $this;
    }

    public function color(int $color): self
    {
        $this->dotColor = $this->dotColorMap[$color];
        return $this;
    }

    public function data(array $data)
    {
        foreach ($data as $key => $value) {
            if (!array_key_exists('text', $value) || !array_key_exists('description', $value) || !array_key_exists('time', $value)) {
                throw new \Exception('第' . $key . '个元素缺少指定的键名');
            }
        }
        $this->data = $data;
        return $this;
    }

    public function display()
    {
        if (empty($this->data)) {
            throw new \Exception('没有数据');
        }
        $this->html = '<ul class="lyear-timeline lyear-timeline-left">';
        foreach ($this->data as $v) {
            $color = isset($v['color']) && !empty($v['color']) ? $v['color'] : $this->dotColor;
            $size = isset($v['size']) && !empty($v['size']) ? $v['size'] : $this->dotSize;
            $status = $v['text'];
            $content = $v['description'];
            $time = !is_numeric($v['time']) ? $v['time'] : date('Y-m-d H:i:s', $v['time']);

            $this->html .= <<<HTML
<li class="lyear-timeline-item">
    <div class="lyear-timeline-item-dot">
        <span class="$color $size"></span>
    </div>
    <div class="lyear-timeline-item-content">
        <p class="mb-1"><strong>$status</strong></p>
        <p class="mb-0">$content</p>
        <p><time class="mb-3">$time</time></p>
    </div>
</li>
HTML;

        }
        $this->html .= '</ul>';
        return parent::display(); // TODO: Change the autogenerated stub
    }

    /*<ul class="lyear-timeline lyear-timeline-left">
                  <li class="lyear-timeline-item">
                    <div class="lyear-timeline-item-dot">
                      <span class="danger"></span>
                    </div>
                    <div class="lyear-timeline-item-content">
                      <p class="mb-1"><strong>运输中</strong></p>
                      <p class="mb-0">北京转运中心 已收入</p>
                      <p><time class="mb-3">2020-05-22 17:38</time></p>
                    </div>
                  </li>
                </ul>*/
}